package com.yh.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.yh.bean.Category;
import com.yh.bean.OfficeObject;
import com.yh.bean.OfficeObjectApply;
import com.yh.service.impl.OfficeObjectService;

@Controller
public class OfficeObjectOperate {

	
	@Autowired
	private OfficeObjectService oos;
	
	//查询所有分类
	@RequestMapping("/querycategory")
	@ResponseBody
	public List<Category> querycategory(){
		List<Category> ctglist=oos.querycategory();
		return ctglist;
	}

	//根据条件查询物品
	@RequestMapping("/queryoobycondition")
	@ResponseBody
	public Map queryoobycondition(OfficeObject oo,int curr){
		System.out.println(oo);
		Page page=PageHelper.startPage(curr, 10);
		List<OfficeObject> oolist=oos.queryoobycondition(oo);
		Map map=new HashMap();
		long totle=page.getTotal();
		System.out.println(oolist);
		map.put("oolist", oolist);
		map.put("totle", totle);
		return map;
	}
	//根据条件查询借出物品
	@RequestMapping("/queryooabycondition")
	@ResponseBody
	public Map queryooabycondition(OfficeObjectApply ooa,OfficeObject oo,int curr){
		
		System.out.println(oo);
		ooa.setOo(oo);
		System.out.println(ooa);
		Page page=PageHelper.startPage(curr, 10);
		List<OfficeObjectApply> ooalist=oos.queryooabycondition(ooa);
		Map map=new HashMap();
		long totle=page.getTotal();
		map.put("ooalist", ooalist);
		map.put("totle", totle);
		return map;
	}
	
	//租用物品
	@RequestMapping("/rentoo")
	@ResponseBody
	public int rentoo(OfficeObjectApply ooa){
		System.out.println(ooa);
		//获取当前时间作为租用时间
		Date date = new Date();
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		ooa.setOoa_applytime(sdf.format(date));
		//租用物品
		int i=oos.rentoo(ooa);
		//获取租用物品的id
		int ooid=ooa.getOoa_ooid();
		//根据id修改库存
		int a=oos.substore(ooa);
		return i;
	}
	
	//添加物品
	@RequestMapping("/insertoo")
	@ResponseBody
	public int insertoo(OfficeObject oo){
		System.out.println(oo);
		int i=oos.insertoo(oo);
		return i;
	}
	
	//添加分类
	@RequestMapping("/insertcategory")
	@ResponseBody
	public int insertcategory(Category ctg){
		System.out.println(ctg);
		int i=oos.insertcategory(ctg);
		return i;
	}
	
	//修改物品
	@RequestMapping("/updateoo")
	@ResponseBody
	public int updateoo(OfficeObject oo){
			System.out.println(oo);
			int i=oos.updateoo(oo);
			return i;
	}
	//删除物品
	@RequestMapping("/deleteoo")
	@ResponseBody
	public int deleteoo(int oo_id){
		System.out.println("删除物品="+oo_id);
		int i=oos.deleteoo(oo_id);
		return i;
	}
	//查新借出物品
	@RequestMapping("/sendoo")
	@ResponseBody
	public List<OfficeObjectApply> sendoo(OfficeObjectApply ooa){
		System.out.println(ooa);
		List<OfficeObjectApply> ooalist=oos.sendoo(ooa);
		return ooalist;
	}
	
	//归还物品
	@RequestMapping("/sendofficeobject")
	@ResponseBody
	public int sendofficeobject(OfficeObjectApply ooa){
		System.out.println(ooa);
		//归还物品
		int i=oos.sendofficeobject(ooa);
		//获取租用物品的id
		int ooid=ooa.getOoa_ooid();
		//根据id修改库存
		oos.addstore(ooa);
		//用户借用表里的借用数量减
			
		//删除数据库里借用物品数量为0的数据
		oos.delooa();
		return i;
	}
}
